const passport = require('passport');
const { ExtractJwt, Strategy } = require('passport-jwt');

const jwtOptions = {
    secretOrKey: 'N4w!x9Q2s@1T',
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
};

const jwtStrategy = new Strategy(jwtOptions, (jwt_payload, done) => {
    const user = { id: jwt_payload.sub };

    if (user) {
        return done(null, user);
    } else {
        return done(null, false);
    }
});

passport.use(jwtStrategy);

const authenticate = passport.authenticate('jwt', { session: false });

module.exports = { authenticate };
